package com.sinoglobal.ningxia.activity.interestingGame;

import java.text.MessageFormat;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.AnswerSourceVo;
import com.sinoglobal.ningxia.beans.InteractionAnswerListItemsVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.AbstractActivity;
import com.sinoglobal.ningxia.frame.ShareAbstractActivity;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.thread.ItktAsyncTask;
import com.sinoglobal.ningxia.utils.FlyUtil;

/**
 * @author sbk
 * @createdate 2013-12-20 下午2:34:08
 * @Description:成绩单
 */
public class AnswerResultActivity extends ShareAbstractActivity implements OnClickListener {
	private String deti; // 详情
	private int percentage; // 正确率
	// private int win; // 击败了多少人
	private int yCount; // 答对了几道 题
	private TextView ivSource, ivNum;
	private ArrayList<InteractionAnswerListItemsVo> aList;

	// private String fenxiang;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//Auto-generated method stub
		super.onCreate(savedInstanceState);
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;

		int height = (int) (1.136 * screenWidth);

		setContentView(R.layout.interaction_answer_last_layout);
		LinearLayout bgLayout = (LinearLayout) findViewById(R.id.l10);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth, height);
		bgLayout.setLayoutParams(params);

		percentage = getIntent().getIntExtra("acc", 0);
		yCount = getIntent().getIntExtra("yCount", 0);
		deti = getIntent().getStringExtra("deti");

		ivSource = (TextView) findViewById(R.id.iv7);
		ivNum = (TextView) findViewById(R.id.iv8);
		// ivWin = (TextView) findViewById(R.id.iv9);
		titleView.setText(getString(R.string.report_card));
		templateButtonRight.setBackgroundResource(R.drawable.img_right_share);
		templateButtonRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String info=getString(R.string.share_report_card);			
				showPopwindow(true,MessageFormat.format(info, yCount,percentage), null, ConnectionUtil.MicDoorAdress);
//				showPopwindow(true, "我使用红枸杞疯狂答题中答对" + yCount + "题，正确率" + percentage + " ！来自@宁夏红枸杞", null, ConnectionUtil.MicDoorAdress);
			}
		});

		findViewById(R.id.btn_call).setOnClickListener(this);

		if (percentage != 100) {
			findViewById(R.id.btn).setVisibility(View.VISIBLE);
			findViewById(R.id.btn).setOnClickListener(this);
		}
		loadData();
		setView();
	}

	private void loadData() {
		new ItktAsyncTask<Void, Void, AnswerSourceVo>() {
			@Override
			public AnswerSourceVo before(Void... params) throws Exception {
				//Auto-generated method stub
				return RemoteImpl.getInstance().getAnswerSource(deti, String.valueOf(percentage));
			}

			@Override
			public void after(AnswerSourceVo result) {
				if (Code.CODE_SUCCESS != result.getCode()) {
					showShortToastMessage(result.getMessage());
					return;
				}
				// win = (int) (Double.valueOf(result.getPaiming()) * 100);
				// fenxiang = result.getFenxiang();
			}

			@Override
			public void exception() {
				//Auto-generated method stub

			}
		}.execute();
	}

	/**
	 * @author sbk
	 * @createdate 2013-12-20 下午3:53:25
	 * @Description: 填充页面
	 * 
	 */
	protected void setView() {
		// ivSource.setBackgroundResource(getRawForWeather("icon_win_", 50));
		ivSource.setText(percentage + "");
		ivNum.setText(yCount + "");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn: // 查看错题
			aList = (ArrayList<InteractionAnswerListItemsVo>) getIntent().getSerializableExtra("mList");
			Intent intent = new Intent(AnswerResultActivity.this, AnswerLookErrorActivity.class);
			intent.putExtra("mList", aList);
			FlyUtil.intentForward(AnswerResultActivity.this, intent);
			break;
		case R.id.btn_call: // 再来一把
			finish();

			break;

		default:
			break;
		}
	}
}
